let mongoose = require('mongoose');
let ObjectId = mongoose.Schema.Types.ObjectId;
const conn = mongoose.createConnection('mongodb://localhost:27017/db201808',{ useNewUrlParser: true });
let UserSchema = new mongoose.Schema({
    username:{type:String,required:true},
    password:String,
    age:Number,
    createAt:{type:Date,default:Date.now}
},{collection:'user'});
let User = conn.model('User',UserSchema);

let ArticleSchema = new mongoose.Schema({
   title:String,
   content:String,
   user:{type:ObjectId,ref:'User'}
},{collection:'user'});
let Article = conn.model('Article',ArticleSchema);
/** 
(async function(){
    let user = await User.create({username:'王五'});
    let article = await Article.create({title:'标题',content:'内容',user:user._id});
    console.log(article);
})()
**/
(async function(){
   let article = await Article.findById('5c938f14e1c184402429d813').populate('user');
   console.log(article);
   /**
    *  let userId = article.user;
   let author = User.findById(userId);
   console.log(author.username);
    */
  
})()